<?php include('includes/before_html.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <meta name="description" content="" />

        <meta name="keywords" content="" />

        <meta name="author" content="" />

        <?php include('includes/head.php'); ?>

        <title><?php echo $heading ?></title>

    </head>

    <body>

        <div id="wrapper">

            <?php include('includes/header.php'); ?>

            <?php include('includes/nav.php'); ?>

            <div id="content">
                <h3>Friends</h3></br>
                <?php
                $db = mysql_connect(db_server, $db_user, $db_password);
                mysql_select_db($db_dbname);

                $sql = "SELECT DISTINCT U.FirstName, U.LastName, U.RUID FROM Friends F, Users U WHERE F.Person1=" . $_COOKIE["ruid"] . " AND F.Person2=U.RUID AND F.Status='Accepted'";
                $result = mysql_query($sql);

                //output data in a table
                $output = '';
                $output.= "<div id='output'>\n";
                $output.= "<table width='100%'>\n";
                $output.= "<tr>";
               
                $output.="<th>Name</th><th>RUID</th><th>Status</th><th>Time</th><th>Action</th>";
                $output.= "</tr>";
                while ($row = mysql_fetch_array($result)) {
		    #Get status of friend
		    $sql_status = "SELECT S.id, S.Text, S.TimePosted,S.RUID FROM Status S WHERE S.RUID = ".$row['RUID']." ORDER BY S.id DESC";
		    $result_status = mysql_query($sql_status);
		    $row2 = mysql_fetch_array($result_status);
                    $output.= "<tr>\n";
                    $output.= "<td><a href='dashboard.php?ruid=".$row['RUID']."'>".$row['FirstName']." ".$row['LastName']."</a></td>\n";#name
                    $output.= "<td><a href='dashboard.php?ruid=".$row['RUID']."'>".$row['RUID']."</a></td>\n";#RUID
		    $output.= "<td><a href='dashboard.php?ruid=".$row2['RUID']."'>".$row2['Text']."</a></td>\n";#status
		    $output.= "<td><a href='dashboard.php?ruid=".$row2['id']."'>".$row2['TimePosted']."</a></td>\n";#time
                    $output.="<td><a href='friend_request_action.php?rid=".$row['RUID']."&a=0&f=1'>Unfriend</a></td>";#Friend Action
                    $output.= "</tr>\n";
                }
                $output.= '</table></div>';

                $count = mysql_num_rows($result);
                if ($count <= 0)
                    echo "You have no friends";
                else {
                    echo $output;
                }


                mysql_close($db)
                ?>
                <br></br><a href="find_friend.php">Find friends</a>
                <br></br><a href="friend_requests.php">Friend Requests</a>
            </div> <!-- end #content -->

            <?php include('includes/sidebar.php'); ?>

            <?php include('includes/footer.php'); ?>

        </div> <!-- End #wrapper -->

    </body>

</html>